<?php
/**
 *
 * @author Haohao
 * @date 2022-04-18 4:08 下午
 *
 */

namespace app\common\model\tang;


use app\common\impl\Comm;
use app\common\model\CommonBaseModel;
use app\common\model\tang\CostRule;
use think\facade\Db;

class TicketCost extends CommonBaseModel implements Comm
{
    protected $table = 'tang_cost';

    public function getAdminPageData($param = [], $order = 'id desc',$where =[],$with=[])
    {
        $model = self::where($where);
        if (isset($param['merchant_id']) && $param['merchant_id']){
            $model->where('merchant_id','=',$param['merchant_id']);
        }
        if (isset($param['pay_status']) && $param['pay_status']!=null ){
            $model->where('pay_status','=',$param['pay_status']);
        }
        if(isset($param['time'])&&$param['time']){
            $endtime = strtotime('-'.$param['time'].' day');
            $model->whereBetweenTime('create_time','<',$endtime);
            $model->where('pay_status','=',0);
        }
        // TODO: Implement getAdminPageData() method.
        $data = $model->with(['merchant','costType','TicketConfig'])->order($order)->page(PAGE)->limit(LIMIT)->select()->toArray();
        $count = $model->count("id");
        return parent::ajaxResult($data, $count);
    }

    public function delById($ids)
    {
        Db::startTrans();
        try {
            if (is_array($ids)){
                $id=$ids;
            }else{
                $id=@explode(",",$ids);
            }
            $result = self::where('id', 'in',$id)->delete();
            Db::commit();
            if ($result) {
                $CostRulelist = (new CostRule())->where('pid', 'in',$id)->select();
                if(!empty($CostRulelist)){
                    $res = (new CostRule())->where('pid', 'in',$id)->delete();
                }
                return self::JsonReturn("删除成功");
            } else {
                return self::JsonReturn("删除失败",0);
            }
        } catch (\Exception $exception) {
            Db::rollback();
            return self::JsonReturn($exception->getMessage(),0);
        }
        // TODO: Implement delById() method.
    }


    public function doAll($data)
    {
        // TODO: Implement doAll() method.
        return parent::doAllData($data);
    }
    public function merchant(): \think\model\relation\HasOne
    {
        return $this->hasOne(Merchant::class,'id','merchant_id');
    }
    public function costType(): \think\model\relation\HasOne
    {
        return $this->hasOne(CostType::class,'id','cost_type_id');
    }

    public function merchants()
    {
        return $this->hasOne(Merchant::class,'id','merchant_id')->field('id,merchant_sn,name,company,username');
    }

    public function costRule()
    {
        return $this->hasMany(CostRule::class,'pid','id');
    }

    public function costRules()
    {
        $m = $this->hasMany(CostRule::class,'pid','id') ;
        return  $m = $m->with('costType') ;
    }

    public function subjectList()
    {
        return $this->hasOne(SubjectList::class,'id','department_id');
    }
    public function payType()
    {
        return $this->hasOne(SubjectList::class,'id','subject_id');
    }
    public function Cost()
    {
        return $this->hasMany(Cost::class,'pid','id');
    }
    public function TicketConfig()
    {
        return $this->hasOne(TicketConfig::class,'id','ticket_id')->field('id,name');
    }

}